import { Component, OnInit } from '@angular/core';
import {ColumnItem, Course, CourseInfo, CourseSelect, Student, Studentgrade} from '../student';
import {HttpService} from '../http.service';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'app-select-course',
  templateUrl: './select-course.component.html',
  styleUrls: ['./select-course.component.css']
})
export class SelectCourseComponent implements OnInit {
  listOfColumns: ColumnItem[] = [
    {
      name: '学号',
      sortOrder: 'null',
      sortFn: (a: CourseSelect, b: CourseSelect) => a.stuid - b.stuid,
    },
    {
      name: '姓名',
    },
    {
      name: '专业'
    },
    {
      name: '班级'
    },
    {
      name: '成绩',
      sortOrder: 'null',
      sortFn: (a: CourseSelect, b: CourseSelect) => a.score - b.score
    }
    ];
  courseStudent: CourseSelect[] = [];
  course: Course[] = [];
  pageindex = 1;
  pagesize = 10;
  loading = true;
  display: CourseSelect[] = [];
  display1: CourseSelect[] = [];
  searchValue = '';
  searchValue2 = '';
  visible = false;
  visible2 = false;
  course1: CourseInfo = {
    courseid : '',
    coursename: '',
    credit : null ,
    type : '',
};
  courseid: any = String(this.route.snapshot.paramMap.get('courseid'));
  constructor(private httpService: HttpService,
              private route: ActivatedRoute) { }
  getcourse() {
    this.httpService.course_select_courseid(this.courseid).subscribe(
      res => {
        res.data.forEach(row => {
          this.course.push( {
            courseid : row.courseid,
            score : row.score,
            stuid : row.stuid,
          });
        });
        for (const i in this.course) {
          this.httpService.getStudentDetail(this.course[i].stuid).subscribe(
            result => {
              console.log(this.course[i].stuid);
              this.courseStudent.push({
                stuid: this.course[i].stuid,
                name: result.data.name,
                major: result.data.major,
                class: result.data.class,
                score: this.course[i].score,
              });
              if (this.course.length === this.courseStudent.length) {
                this.display = this.courseStudent;
                this.display1 = this.courseStudent;
              }
            });
        }
        this.loading = false;
      }
    );
  }
  reset() {
    this.display = this.display1;
  }
  search() {
    this.visible = false;
    this.display = this.display.filter((item: CourseSelect) => item.major.indexOf(this.searchValue) !== -1) ;
  }
  search2() {
    this.visible2 = false;
    this.display = this.display.filter((item: CourseSelect) => item.class.indexOf(this.searchValue2) !== -1) ;
  }
  getcourse1() {
    this.httpService.getCourseDetail(this.courseid).subscribe(
      res => this.course1 = {
        courseid : res.data.courseid,
        coursename: res.data.coursename,
        credit : res.data.credit,
        type : res.data.type
      }
    );
  }
  ngOnInit(): void {
    this.getcourse();
    this.getcourse1();
  }
}
